﻿/****************************************************************************************************
	Copyright (C) 2010 RapidWebDev Organization (http://rapidwebdev.org)
	Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@RapidWebDev.org

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************************************/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaoJianSoft.Common;
using BaoJianSoft.Common.Data;
using BaoJianSoft.Common.Validation;
using BaoJianSoft.ExtensionModel.Statistic;
using BaoJianSoft.ExtensionModel.Web.Properties;
using BaoJianSoft.RapidWeb;
using BaoJianSoft.RapidWeb.DynamicPages;
using MyControls = BaoJianSoft.RapidWeb.Controls;

namespace BaoJianSoft.ExtensionModel.Web.DynamicPage
{
	/// <summary>
	/// Aggregate panel to preview statistic report.
	/// </summary>
	public abstract class StatisticReportPreviewPanel : AggregatePanelPage
	{
		[Binding]
		protected Panel PanelStatisticReportPreview;

		/// <summary>
		/// 呈现统计表的预览
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		public override void OnLoad(object sender, AggregatePanelPageEventArgs e)
		{
			if (this.PanelStatisticReportPreview != null)
			{
				Table statisticReportPreviewTable = this.CreateStatisticReportPreviewTable();
				this.PanelStatisticReportPreview.Controls.Add(statisticReportPreviewTable);
			}
		}

		/// <summary>
		/// 设置统计表的名称
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		public override void SetupContextTempVariables(object sender, SetupApplicationContextVariablesEventArgs e)
		{
			StatisticDefinition statisticDefinition = this.GetStatisticDefinition();
			e.TempVariables["StatisticDefinition.Id"] = statisticDefinition.Id;
			e.TempVariables["StatisticDefinition.Name"] = statisticDefinition.Name;
		}

		/// <summary>
		/// Get statistic definition for adding filter in or editing the filter.
		/// </summary>
		/// <returns></returns>
		protected abstract StatisticDefinition GetStatisticDefinition();

		private Table CreateStatisticReportPreviewTable()
		{
			StatisticDefinition statisticDefinition = this.GetStatisticDefinition();
			if (statisticDefinition.Columns == null || statisticDefinition.Columns.Length == 0)
				throw new ValidationException("统计表的列未定义，无法进行预览。");

			List<string> columnNames = new List<string>();
			columnNames.Add("");
			columnNames.AddRange(statisticDefinition.Columns.Select(filter => filter.HeaderText));
			columnNames.Add("合计");

			if (statisticDefinition.Rows == null || statisticDefinition.Rows.Length == 0)
				throw new ValidationException("统计表的行未定义，无法进行预览。");

			List<string> rowNames = new List<string>();
			rowNames.AddRange(statisticDefinition.Rows.Select(filter => filter.HeaderText));
			rowNames.Add("合计");

			Table statisticReportTable = new Table { CellPadding = 0, CellSpacing = 0, CssClass = "statisticreport-preview-table" };

			TableRow tableHeaderRow = new TableRow();
			statisticReportTable.Rows.Add(tableHeaderRow);

			foreach (string columnName in columnNames)
			{
				TableHeaderCell tableHeaderCell = new TableHeaderCell();
				Label labelHeaderText = new Label { Text = columnName };
				labelHeaderText.Font.Bold = true;
				tableHeaderCell.Controls.Add(labelHeaderText);
				tableHeaderRow.Cells.Add(tableHeaderCell);
			}

			foreach (string rowName in rowNames)
			{
				TableRow tableDataRow = new TableRow();
				statisticReportTable.Rows.Add(tableDataRow);

				for (int columnIndex = 0; columnIndex < columnNames.Count; columnIndex++)
				{
					string cssClass = "";
					Label labelContent;
					if (columnIndex == 0)
					{
						cssClass = "c1";
						labelContent = new Label { Text = rowName };
						labelContent.Font.Bold = true;
					}
					else
						labelContent = new Label { Text = "X" };

					TableCell tableDataCell = new TableCell();
					tableDataCell.Attributes["class"] = cssClass;
					tableDataCell.Controls.Add(labelContent);
					tableDataRow.Cells.Add(tableDataCell);
				}
			}

			return statisticReportTable;
		}
	}
}

